import random
import time

class RiceField:
    """稻田类，管理水稻生长状态和各种属性"""
    def __init__(self):
        self.growth_stage = 0  # 0: 未播种, 1: 发芽, 2: 幼苗, 3: 分蘖, 4: 抽穗, 5: 成熟
        self.water_level = 50  # 水分含量0-100
        self.fertilizer = 30   # 肥料含量0-100
        self.health = 100      # 健康度0-100，低于30可能生病
        self.pests = 0         # 害虫数量0-100
        self.weeds = 0         # 杂草数量0-100
        self.yield_potential = 0  # 潜在产量
        
    def grow(self):
        """水稻生长函数，根据当前条件决定生长情况"""
        # 检查是否满足生长条件
        if self.growth_stage == 0:
            print("请先播种！")
            return False
            
        # 计算生长因子
        growth_factor = 0
        
        # 水分影响
        if 40 <= self.water_level <= 70:
            growth_factor += 0.3
        elif 20 <= self.water_level < 40 or 70 < self.water_level <= 80:
            growth_factor += 0.15
        else:
            growth_factor -= 0.2
            
        # 肥料影响
        if 20 <= self.fertilizer <= 60:
            growth_factor += 0.3
        elif 10 <= self.fertilizer < 20 or 60 < self.fertilizer <= 80:
            growth_factor += 0.15
        else:
            growth_factor -= 0.1
            
        # 健康度影响
        if self.health >= 80:
            growth_factor += 0.2
        elif self.health >= 50:
            growth_factor += 0.1
        elif self.health >= 30:
            growth_factor += 0.05
        else:
            growth_factor -= 0.3
            
        # 杂草影响
        weed_impact = self.weeds / 100 * 0.5
        growth_factor -= weed_impact
        
        # 害虫影响
        pest_impact = self.pests / 100 * 0.5
        growth_factor -= pest_impact
        
        # 随机事件影响
        event_chance = random.random()
        if event_chance < 0.05:
            print("突发：下了一场及时雨！水分充足，有利于生长！")
            growth_factor += 0.2
            self.water_level = min(100, self.water_level + 30)
        elif event_chance < 0.1:
            print("突发：遇到干旱，水分蒸发很快！")
            growth_factor -= 0.2
            self.water_level = max(0, self.water_level - 30)
        elif event_chance < 0.13:
            print("突发：来了一群鸟，吃掉了一些稻苗！")
            growth_factor -= 0.3
            self.health = max(0, self.health - 20)
        
        # 确定是否生长
        if growth_factor > 0.3 and random.random() < 0.7:
            self.growth_stage += 1
            self.yield_potential += int(growth_factor * 20) + random.randint(5, 15)
            return True
        return False
        
    def get_stage_name(self):
        """返回当前生长阶段的名称"""
        stages = ["未播种", "发芽", "幼苗", "分蘖", "抽穗", "成熟"]
        return stages[self.growth_stage]
        
    def water(self, amount):
        """浇水"""
        self.water_level = min(100, self.water_level + amount)
        print(f"已浇水，当前水分含量: {self.water_level}%")
        
    def add_fertilizer(self, amount):
        """施肥"""
        self.fertilizer = min(100, self.fertilizer + amount)
        print(f"已施肥，当前肥料含量: {self.fertilizer}%")
        
    def remove_weeds(self):
        """除草"""
        removed = random.randint(30, 70)
        self.weeds = max(0, self.weeds - removed)
        print(f"已除草，当前杂草数量: {self.weeds}%")
        
    def pest_control(self):
        """除虫"""
        removed = random.randint(40, 80)
        self.pests = max(0, self.pests - removed)
        print(f"已除虫，当前害虫数量: {self.pests}%")
        
    def seed(self):
        """播种"""
        if self.growth_stage == 0:
            self.growth_stage = 1
            self.yield_potential = 50
            print("播种成功！水稻开始发芽了！")
            return True
        else:
            print("已经播种过了！")
            return False
            
    def harvest(self):
        """收获"""
        if self.growth_stage == 5:
            # 根据各种因素计算实际产量
            actual_yield = int(self.yield_potential * (self.health / 100) * 
                              (1 - self.weeds / 200) * (1 - self.pests / 200) + 
                              random.randint(-20, 20))
            actual_yield = max(0, actual_yield)
            print(f"收获成功！本次收获了 {actual_yield} 公斤水稻！")
            return actual_yield
        else:
            print("水稻还没成熟，不能收获！")
            return 0
            
    def daily_changes(self):
        """每天的自然变化"""
        # 水分自然减少
        self.water_level = max(0, self.water_level - random.randint(5, 15))
        
        # 杂草生长
        self.weeds = min(100, self.weeds + random.randint(3, 10))
        
        # 可能滋生害虫
        if random.random() < 0.3:
            self.pests = min(100, self.pests + random.randint(2, 8))
            
        # 肥料消耗
        if self.growth_stage > 0:
            self.fertilizer = max(0, self.fertilizer - random.randint(2, 7))
            
        # 健康度变化
        if self.pests > 60 or self.weeds > 70:
            self.health = max(0, self.health - random.randint(5, 15))
        elif 30 <= self.pests <= 60 or 40 <= self.weeds <= 70:
            self.health = max(0, self.health - random.randint(1, 5))
        elif self.water_level < 20 or self.water_level > 80:
            self.health = max(0, self.health - random.randint(3, 8))
        elif self.fertilizer < 10:
            self.health = max(0, self.health - random.randint(2, 6))
        elif self.growth_stage > 0 and random.random() < 0.2:
            self.health = min(100, self.health + random.randint(1, 3))
            
        # 检查是否生病
        if self.health < 30 and random.random() < 0.4:
            print("警告：水稻生病了！健康度下降！")
            self.health = max(0, self.health - random.randint(10, 20))


class TenFarmersGame:
    """十个勤天种稻游戏主类"""
    def __init__(self):
        self.rice_field = RiceField()
        self.day = 1
        self.total_harvest = 0
        self.season = 1
        self.money = 100  # 初始资金
        self.tools = {"浇水壶": 1, "镰刀": 1, "除虫剂": 1}
        self.farmers = ["蒋敦豪", "鹭卓", "李耕耘", "李昊", "赵一博", 
                       "卓沅", "赵小童", "何浩楠", "陈少熙", "王一珩"]
        print("欢迎来到《十个勤天种稻记》游戏！")
        print("你将扮演十个勤天的成员，体验水稻种植的全过程。")
        print("目标是种出优质高产的水稻，获得好收成！")
        print("-" * 50)
        
    def show_status(self):
        """显示当前状态"""
        print(f"\n=== 第 {self.season} 季 - 第 {self.day} 天 ===")
        print(f"水稻状态: {self.rice_field.get_stage_name()}")
        print(f"水分: {self.rice_field.water_level}% | 肥料: {self.rice_field.fertilizer}%")
        print(f"健康度: {self.rice_field.health}% | 害虫: {self.rice_field.pests}% | 杂草: {self.rice_field.weeds}%")
        print(f"资金: {self.money}元 | 总收获: {self.total_harvest}公斤")
        print("-" * 50)
        
    def show_actions(self):
        """显示可执行的操作"""
        print("请选择要进行的操作:")
        print("1. 查看状态")
        print("2. 播种 (仅当未播种时)")
        print("3. 浇水")
        print("4. 施肥 (花费10元)")
        print("5. 除草")
        print("6. 除虫 (花费15元)")
        print("7. 等待一天")
        print("8. 收获 (仅当成熟时)")
        print("9. 购买工具 (提高效率)")
        print("10. 结束游戏")
        return input("请输入操作编号: ")
        
    def farmer_action(self):
        """随机选择一个成员进行随机帮助"""
        farmer = random.choice(self.farmers)
        action = random.choice(["浇水", "施肥", "除草", "除虫"])
        
        if action == "浇水":
            self.rice_field.water(random.randint(10, 25))
            print(f"好消息！{farmer} 帮你浇了水！")
        elif action == "施肥":
            self.rice_field.add_fertilizer(random.randint(5, 15))
            print(f"好消息！{farmer} 帮你施了肥！")
        elif action == "除草":
            removed = random.randint(20, 50)
            self.rice_field.weeds = max(0, self.rice_field.weeds - removed)
            print(f"好消息！{farmer} 帮你除了草，当前杂草数量: {self.rice_field.weeds}%")
        elif action == "除虫":
            removed = random.randint(20, 50)
            self.rice_field.pests = max(0, self.rice_field.pests - removed)
            print(f"好消息！{farmer} 帮你除了虫，当前害虫数量: {self.rice_field.pests}%")
            
    def buy_tool(self):
        """购买工具"""
        print("\n--- 工具商店 ---")
        print("1. 高级浇水壶 (30元) - 提高浇水效率")
        print("2. 优质肥料 (40元) - 提高施肥效果")
        print("3. 高效除草剂 (50元) - 提高除草效果")
        print("4. 强力杀虫剂 (60元) - 提高除虫效果")
        print("5. 返回")
        
        choice = input("请选择要购买的工具: ")
        if choice == "1" and self.money >= 30:
            self.money -= 30
            self.tools["浇水壶"] += 1
            print("购买高级浇水壶成功！浇水效果提升！")
        elif choice == "2" and self.money >= 40:
            self.money -= 40
            self.tools["肥料"] = self.tools.get("肥料", 0) + 1
            print("购买优质肥料成功！施肥效果提升！")
        elif choice == "3" and self.money >= 50:
            self.money -= 50
            self.tools["除草剂"] = self.tools.get("除草剂", 0) + 1
            print("购买高效除草剂成功！除草效果提升！")
        elif choice == "4" and self.money >= 60:
            self.money -= 60
            self.tools["杀虫剂"] = self.tools.get("杀虫剂", 0) + 1
            print("购买强力杀虫剂成功！除虫效果提升！")
        elif choice == "5":
            return
        else:
            print("无法购买，请检查资金是否充足或选择是否正确！")
            
    def process_action(self, choice):
        """处理玩家选择的操作"""
        if choice == "1":
            self.show_status()
            
        elif choice == "2":
            if self.rice_field.seed():
                self.money -= 20  # 种子成本
                
        elif choice == "3":
            # 根据工具等级决定浇水量
            water_amount = 20 + self.tools["浇水壶"] * 5
            self.rice_field.water(water_amount)
            
        elif choice == "4":
            if self.money >= 10:
                self.money -= 10
                # 根据工具等级决定施肥量
                fertilizer_amount = 15 + self.tools.get("肥料", 0) * 8
                self.rice_field.add_fertilizer(fertilizer_amount)
            else:
                print("资金不足，无法施肥！")
                
        elif choice == "5":
            # 根据工具等级决定除草效果
            base_remove = 40
            bonus = self.tools.get("除草剂", 0) * 15
            removed = random.randint(base_remove, base_remove + bonus)
            self.rice_field.weeds = max(0, self.rice_field.weeds - removed)
            print(f"已除草，当前杂草数量: {self.rice_field.weeds}%")
            
        elif choice == "6":
            if self.money >= 15:
                self.money -= 15
                # 根据工具等级决定除虫效果
                base_remove = 50
                bonus = self.tools.get("杀虫剂", 0) * 20
                removed = random.randint(base_remove, base_remove + bonus)
                self.rice_field.pests = max(0, self.rice_field.pests - removed)
                print(f"已除虫，当前害虫数量: {self.rice_field.pests}%")
            else:
                print("资金不足，无法除虫！")
                
        elif choice == "7":
            print("等待一天...")
            # 水稻生长
            grew = self.rice_field.grow()
            if grew:
                print(f"水稻生长了！现在是{self.rice_field.get_stage_name()}阶段。")
            else:
                print("水稻今天没有明显生长。")
                
            # 每天的自然变化
            self.rice_field.daily_changes()
            
            # 随机一个成员帮忙
            if random.random() < 0.5:
                self.farmer_action()
                
            self.day += 1
            
            # 检查是否成熟超过5天未收获
            if self.rice_field.growth_stage == 5 and self.day % 5 == 0:
                print("警告：水稻已经成熟很久了，再不收获会减产！")
                self.rice_field.yield_potential = max(0, self.rice_field.yield_potential - 10)
                
        elif choice == "8":
            harvest = self.rice_field.harvest()
            if harvest > 0:
                self.total_harvest += harvest
                self.money += harvest * 3  # 每公斤卖3元
                # 开始新的一季
                self.rice_field = RiceField()
                self.day = 1
                self.season += 1
                print(f"第 {self.season} 季开始了！")
                
        elif choice == "9":
            self.buy_tool()
            
        elif choice == "10":
            print("\n--- 游戏结束 ---")
            print(f"你共完成了 {self.season - 1} 季水稻种植")
            print(f"总收获: {self.total_harvest} 公斤")
            print(f"最终资金: {self.money} 元")
            
            # 评价
            if self.total_harvest >= 500:
                print("太棒了！你是一个优秀的稻农，达到了十个勤天的水平！")
            elif self.total_harvest >= 300:
                print("做得不错！你已经掌握了种稻的基本技巧。")
            elif self.total_harvest >= 100:
                print("还可以，继续努力，你会成为更好的稻农！")
            else:
                print("加油啊！多学习种稻知识，下次一定会有好收成！")
                
            return False
            
        else:
            print("无效的操作，请重新选择！")
            
        return True
        
    def start(self):
        """开始游戏"""
        running = True
        while running:
            self.show_status()
            action = self.show_actions()
            running = self.process_action(action)
            time.sleep(1)  # 稍微延迟一下，让玩家看得清楚


if __name__ == "__main__":
    game = TenFarmersGame()
    game.start()
